package com.fll.fangyishare.service.impl;

import com.fll.fangyicommon.entity.share.Liked;
import com.fll.fangyicommon.entity.share.Oss;
import com.fll.fangyicommon.entity.share.Share;
import com.fll.fangyishare.dao.LikedDao;
import com.fll.fangyishare.dao.OssDao;
import com.fll.fangyishare.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fll
 * create 2023/3/25
 */
@Service
@Slf4j
public class LikedServiceImpl implements LikedService {

    @Autowired
    private LikedDao likedDao;


    /**
     * 获取当前登录用户的点赞的share
     * 先查出点赞过的share_id，然后再去查share即可
     * @param userId
     * @return
     */
    @Override
    public String getLikeByUser(Integer userId){
        ArrayList<Integer> shareIds = likedDao.getLikeByUser(userId);
        if(shareIds != null || shareIds.size() != 0){
            StringBuilder sb = new StringBuilder("(");
            for(Integer id : shareIds){
                sb.append(id);
                sb.append(",");
            }
            sb.deleteCharAt(sb.length() - 1);
            sb.append(")");
            return sb.toString();
        }
        return null;
    }

    /**
     * 判断是否已点赞
     * @param userId
     * @param shareId
     * @return
     */
    @Override
    public Boolean isLike(Integer userId, Integer shareId){
        Liked like = likedDao.isLike(userId, shareId);
        if (like == null){
            return false;
        }
        return true;
    }

    /**
     * 添加一条like记录
     * @param liked
     * @return
     */
    @Override
    public Integer addLiked(Liked liked){
        Integer num = likedDao.addLiked(liked);
        if (num != 0){
            log.info("添加like成功===>{},记录数===>{}",liked, num);
            return num;
        }
        log.warn("添加like失败");
        return -1;
    }

    /**
     * 取消点赞，即删除记录
     * @param liked
     * @return
     */
    public Integer cancelLiked(Liked liked){
        Integer num = likedDao.cancelLiked(liked);
        if (num != 0){
            log.info("已删除点赞");
            return num;
        }
        log.warn("删除点赞失败");
        return -1;
    }


    /**
     * 获取点赞总数
     * @param shareId
     * @return
     */
    public Integer getLikedCount(Integer shareId){
        return likedDao.getLikedCount(shareId);
    }

}
